<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <!--行工具栏模板-->
        <script type="text/html" id="lineBar">
            <div class="layui-clear-space">
                <a class="layui-btn layui-btn-xs layui-bg-red" lay-event="delete">
                    <i class="layui-icon layui-icon-delete"></i>删除</a>
            </div>
        </script>
        <!--头部工具栏模板-->
        <script type="text/html" id="headerBar">
            <div class="layui-clear-space">
                <a class="layui-btn layui-btn-sm" lay-event="upload">
                    <i class="layui-icon layui-icon-upload-drag"></i>上传
                </a>
            </div>
        </script>


        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>


    </div>
</div>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        let $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        // 已知数据渲染
        table.render({
            elem: '#currentTableId',
            url: '/slide/page',
            toolbar: '#headerBar',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field: 'title', title: '标题'},
                {
                    field: 'uuid', title: '图片', minWidth: 160, event:'editImg', templet: function (row) {
                        return '<div>' +
                            '<img src="'+row.uuid+'" style="width: 80px;height: 80px;">'+
                            '</div>'
                    }
                },
                {field: 'createTime', title: '上传时间'},
                {field: 'updateTime', title: '更新时间'},
                {title: '操作', minWidth: 200, toolbar: '#lineBar', align: "center"}
            ]],
            limits: [5, 10, 15],
            limit: 5,
            page: true,
            skin: 'line'
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'upload') {  // 监听添加操作
                let index = layer.open({
                    title: '图片上传',
                    type: 2,
                    shade: 0.6,
                    shadeClose: true,
                    area: ['30%', '70%'],
                    content: 'upload.html',
                    end: function () {
                        table.reload("currentTableId")  // 重载表格数据
                    }
                });
            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            let data = obj.data;
            if (obj.event === 'edit') {
                let carouseId = data.carouseId
                // 发送请求   更新图片时间
                $.post("/slide/update",{carouseId},function (res){
                    if (res.code==0){
                        layer.msg(res.msg,function (){
                            table.reload("currentTableId")
                        })
                    }else{
                        layer.msg(res.msg)
                    }
                })
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    let uuid = data.uuid
                    $.post('/slide/delete',{uuid},function (res){
                        if (res.code==0){
                            layer.msg(res.msg,{icon:1},function (){
                                table.reload("currentTableId")
                            })
                        }else {
                            layer.msg(res.msg,{icon:2})
                        }
                    })
                    layer.close(index);
                });
            }else  if (obj.event === 'editImg'){
                sessionStorage.setItem("carouseId",data.carouseId);
                sessionStorage.setItem("uuid",data.uuid);
                let index = layer.open({
                    title: '图片上传',
                    type: 2,
                    shade: 0.6,
                    shadeClose: true,
                    area: ['30%', '70%'],
                    content: 'upload.html',
                    end: function () {
                        table.reload("currentTableId")  // 重载表格数据
                    }
                });
            }
        });

    });
</script>

</body>
</html>